package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter;

import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.IAboutDeskMediaContract;

/**
 * Created by Administrator on 2018/8/14.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class AboutDeskMediaPersenter implements IAboutDeskMediaContract.IAboutDeskMediaPersenter {
    private IAboutDeskMediaContract.IAboutDeskMediaView mIAboutDeskMediaView;

    public AboutDeskMediaPersenter(IAboutDeskMediaContract.IAboutDeskMediaView mIAboutDeskMediaView) {
        this.mIAboutDeskMediaView = mIAboutDeskMediaView;
    }

    @Override
    public void initTitle() {
        mIAboutDeskMediaView.showTitle();
    }

    @Override
    public void initWebview(final WebView mWebView) {
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);                                                       //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true);                                                  // 缩放至屏幕的大小
        webSettings.setDisplayZoomControls(true);                                                   //隐藏原生的缩放控件
        webSettings.setAllowFileAccess(true);                                                       //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);                                 //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true);                                              //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");                                            //设置编码格式

        //开启DOM storage API功能
        webSettings.setDomStorageEnabled(true);
        //开启database storage API功能
        webSettings.setDatabaseEnabled(true);
        //开启Application Cache功能
        webSettings.setAppCacheEnabled(true);
        mWebView.loadUrl("http://www.hzdeskmedia.cn/");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                // showErrorPage();//显示错误页面
                //mWebView.loadUrl("http://www.hzdeskmedia.cn/");
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
                    mIAboutDeskMediaView.showProgressBar();
                    mIAboutDeskMediaView.setMyProgress(newProgress);
                } else {
                    mIAboutDeskMediaView.hidenProgressBar();
                }
            }
        });
    }


    @Override
    public void webviewonResume(WebView mWebView) {
        mWebView.onResume();
    }

    @Override
    public void webviewonPause(WebView mWebView) {
        mWebView.onPause();
    }

    @Override
    public void webviewonDestroy(WebView mWebView) {
        if (mWebView != null) {
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();

            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
    }
}
